package com.yu.test;

import org.junit.Test;
import org.mockito.Mockito;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;

/**
 * Created by yuxiaolong on 2015/4/8.
 */
public class StudyMock {

    @Test
    public void testMock(){
        System.out.println("--->into test");
        List mock = Mockito.mock(List.class);
        mock.add(1);
        System.out.println(mock.size());
        mock.clear();
        Mockito.verify(mock).add(1);
        Mockito.verify(mock).clear();
    }

    @Test(expected = IOException.class)
    public void testWhenThenThrow() throws IOException {
        OutputStream outputStream = Mockito.mock(OutputStream.class);
        OutputStreamWriter writer = new OutputStreamWriter(outputStream);
        Mockito.doThrow(new IOException()).when(outputStream).close();
        outputStream.close();
    }

    public void test2(){
        Iterator iterator = Mockito.mock(Iterator.class);
        Mockito.when(iterator.next()).thenReturn("hello");
    }

}
